/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trabalho1_sd_java;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.rmi.server.SocketSecurityException;
import java.util.Random;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author gui
 */
public class Cliente_Java {

    public static final int NUM_PORTA = 14568;
    public static final int TAM_VET = 125;
    public static final int NUM_CLIENTES = 8;
    private InetAddress endServidor;
    private Socket socketEnvio;
    private ServerSocket socketEspera;
    private int[] vet;
    private int[] vetTotal;
    private String nomeHost;

    public Cliente_Java(String endIpServidor) {

        try {
            endServidor = InetAddress.getByName(endIpServidor);
        } catch (UnknownHostException ex) {
            Logger.getLogger(Cliente_Java.class.getName()).log(Level.SEVERE, null, ex);
        }

        vet = new int[TAM_VET];
        vetTotal = new int[TAM_VET * NUM_CLIENTES];

        try {
            nomeHost = InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException ex) {
            Logger.getLogger(Cliente_Java.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        System.out.print("\n\n ********** Cliente [J] - " + nomeHost + " ***********");
    }

    public void comunicar() {

        Random rd = new Random();

        for (int i = 0; i < vet.length; i++) {

            vet[i] = rd.nextInt(1000);
        }

        System.out.print("\n\n [J] >> " + nomeHost + ": Vetor gerado: ");

        for (int i : vet) {

            System.out.print(i + " ");
        }

        try {

            socketEnvio = new Socket(endServidor, NUM_PORTA);

            InputStream sockIn = socketEnvio.getInputStream();
            OutputStream sockOut = socketEnvio.getOutputStream();

            DataOutputStream sockOutData = new DataOutputStream(sockOut);

            for (int i : vet) {

                sockOutData.writeInt(i);
            }

            Scanner leitorResposta = new Scanner(sockIn);

            System.out.print("\n\n [J] >>" + nomeHost + ": Resposta do servidor: " + leitorResposta.nextLine());

        } catch (IOException ex) {
            Logger.getLogger(Cliente_Java.class.getName()).log(Level.SEVERE, null, ex);
        } finally {

            try {
                socketEnvio.close();
            } catch (IOException ex) {
                Logger.getLogger(Cliente_Java.class.getName()).log(Level.SEVERE, null, ex);
            }
        }


        try {
            socketEspera = new ServerSocket(NUM_PORTA + 1);

            System.out.print("\n\n [J] >>" + nomeHost + ": esperando por requisicoes na porta " + (NUM_PORTA + 1) + ".");

            Socket socketRecebimento = socketEspera.accept();

            InputStream sockIn = socketRecebimento.getInputStream();
            OutputStream sockOut = socketRecebimento.getOutputStream();
            
            DataInputStream sockInData = new DataInputStream(sockIn);
            
            for(int i = 0; i < vetTotal.length; i++){
                
                vetTotal[i] = sockInData.readInt();
            }
            
            PrintWriter escritorResposta = new PrintWriter(sockOut, true);
            
            escritorResposta.print("[J] @" + nomeHost + ": Vetor total recebido.");
            
            System.out.print("\n\n [J] >> " + nomeHost + " Vetor total ordenado: ");
            
            for(int i : vetTotal){
                
                System.out.print(i);
            }

        } catch (IOException ex) {
            Logger.getLogger(Cliente_Java.class.getName()).log(Level.SEVERE, null, ex);
        } finally{
            try {
                socketEspera.close();
            } catch (IOException ex) {
                Logger.getLogger(Cliente_Java.class.getName()).log(Level.SEVERE, null, ex);
            }
        }


    }
}
